import Vue from 'vue';
import Axios from 'axios';
import Qs from 'qs';
import VModal from 'vue-js-modal';
import '../basic/basic.css';
import '../theme/fonts/iconfont.css';
import './css/list.css';
import paging from '../components/pageing/paging.js';
Vue.use(VModal,{dialog:true});
new Vue({
    el:'#louie',
    data:{
        //--公共--
        url:$('#louie').attr('data-url'),
        //顶部导航
        nav:[
            {name:'门店列表',icon:'iconfont icon-mendian',value:'list',status:1,active:1},
            {name:'审核门店',icon:'iconfont icon-operator-check',value:'audit',status:2,active:0},
            {name:'禁用门店',icon:'iconfont icon-jinyong',value:'forbid',status:4,active:0},
            {name:'经营范围(类型)',icon:'iconfont icon-jingyingfanweibiangeng',value:'manage',active:0}
        ],
        nav_index:0,
        //门店列表
        total:0,  //列表总数
        list:[],
        returnNum:10,  //请求数据数量
        pageNum:1,  //当前page页面
        //搜索
        search:0,
        search_type:'id',
        search_keyword:'',  //搜索关键词
        //经营范围
        manage_nav:[
            {name:'经营范围',value:'manage',status:1,active:1},
            {name:'等待审核',value:'audit',status:2,active:0}
        ],
        manage_nav_index:0,
        manage_list:[],
        manage_name:'',
    },
    components:{
        paging:paging
    },
    created:function(){
        this.getTotal();
        this.li();
        this.manageList();
    },
    methods:{
        //nav切换
        changeNav:function(index){
            if(this.nav_index != index){
                this.nav[this.nav_index].active = 0;
                this.nav[index].active = 1;
                this.nav_index = index;
                this.search = 0;
                if(this.nav[index].status != undefined){
                    this.pageNum = 1;
                    this.getTotal();
                    this.li();
                }
            }
        },
        //获取列表总数
        getTotal:function(){
            var data = {
                pages:'store',
                doing:'getTotal',
                status:this.nav[this.nav_index].status
            }
            if(this.search){
                data.search_keyword = this.search_keyword;
                data.search_type = this.search_type;
            }
            Axios.post(this.url,Qs.stringify(data))
                .then((res)=>{
                    if(res.data.status == 'success'){
                        this.total = parseInt(res.data.data);
                    }
                })
                .catch(error=>console.log(error));
        },
        //list获取门店列表
        li:function(){
            var data = {
                pages:'store',
                doing:'li',
                limit:[(this.pageNum-1)*this.returnNum,this.returnNum],
                status:this.nav[this.nav_index].status
            }
            if(this.search){
                data.search_keyword = this.search_keyword;
                data.search_type = this.search_type;
            }
            Axios.post(this.url,Qs.stringify(data))
                .then((res)=>{
                    if(res.data.status == 'success'){
                        this.list = res.data.data;
                    }else{
                        this.list = [];
                    }
                })
                .catch(error=>console.log(error));
        },
        //点击分页
        changePage:function(page){
            this.pageNum = page;
            this.li();
        },
        //点击通过门店审核
        clickPass:function(index){
            this.changeStatus(1,this.list[index].id);
        },
        //点击拒绝门店审核
        clickRefuse:function(index){
            this.confirm('确定拒绝该门店审核吗？',(res)=>{
                if(res) this.changeStatus(3,this.list[index].id);
            });
        },
        //点击删除门店
        clickDelete:function(index){
            this.confirm('确定删除该门店审核吗？',(res)=>{
                if(res) this.changeStatus(9,this.list[index].id);
            });
        },
        //点击锁定门店
        clickLock:function(index){
            this.confirm('确定锁定该门店吗？',(res)=>{
                if(res) this.changeStatus(4,this.list[index].id);
            });
        },
        //点击解锁门店
        clickUnLock:function(index){
            this.confirm('确定解锁该门店吗？',(res)=>{
                if(res) this.changeStatus(1,this.list[index].id);
            });
        },
        //更改门店状态
        changeStatus:function(status,store_id){
            Axios.post(this.url,Qs.stringify({
                pages:'store',
                doing:'changeStatus',
                status,
                store_id
            }))
                .then((res)=>{
                    if(res.data.status == 'success'){
                        this.getTotal();
                        this.li();
                    }else{
                        this.alert('操作失败');
                    }
                })
                .catch(error=>console.log(error));
        },
        //搜索门店
        searchSubmit:function(){
          if(this.search_keyword != ''){
            this.search = 1;
            this.getTotal();
            this.li();
          }
        },
        //点击切换经营范围导航
        changeManageNav:function(index){
            if(this.manage_nav_index != index){
                this.manage_nav[this.manage_nav_index].active = 0;
                this.manage_nav[index].active = 1;
                this.manage_nav_index = index;
                this.manageList();
            }
        },
        //获取系统经营范围
        manageList:function(){
            Axios.post(this.url,Qs.stringify({
                pages:'storeManage',
                doing:'findAll',
                status:this.manage_nav[this.manage_nav_index].status
            }))
                .then(res=>{
                    if(res.data.status == 'success'){
                        this.manage_list = res.data.data;
                    }else{
                        this.manage_list = [];
                    }
                })
                .catch(error=>console.log(error));
        },
        //新增经营范围
        addManage:function(){
          if(this.manage_name == ''){
              this.alert('请输入新增内容');
              return false;
          }
          Axios.post(this.url,Qs.stringify({
               pages:'storeManage',
               doing:'add',
               name:this.manage_name
          }))
              .then((res)=>{
                  if(res.data.status == 'success'){
                    this.manage_name = '';
                    this.manageList();
                  }else{
                      this.alert(res.data.err_msg);
                  }
              })
              .catch(error=>console.log(error));
        },
        //点击经营范围
        clickManage:function(index){
            var manage_id = this.manage_list[index].id;
            if(this.manage_nav[this.manage_nav_index].value == 'manage') {
                this.confirm('确定要删除该分类吗？',(res)=>{
                    if(res){
                        this.changeManageStatus(9,manage_id);
                    }
                });
            }else{
                var self = this;
                this.$modal.show('dialog',{
                    text:'操作',
                    buttons:[
                        {
                            title: '取消',
                            handler:function(){
                                self.$modal.hide('dialog');
                            }
                        },
                        {
                            title: '删除',
                            default: true,
                            handler:function(){
                                self.$modal.hide('dialog');
                                self.changeManageStatus(9,manage_id);
                            }
                        },
                        {
                            title:'通过审核',
                            default:true,
                            handler:function(){
                                self.$modal.hide('dialog');
                                self.changeManageStatus(1,manage_id);
                            }
                        }
                    ]
                });
            }
        },
        //修改经营范围状态
        changeManageStatus:function(status,manage_id){
            Axios.post(this.url,Qs.stringify({
                pages:'storeManage',
                doing:'changeStatus',
                status,
                manage_id
            }))
                .then(res=>{
                    if(res.data.status == 'success'){
                        this.manageList();
                    }else{
                        this.alert('操作失败');
                    }
                })
                .catch(error=>console.log(error));
        },
        //alert
        alert:function(text){
            this.$modal.show('dialog',{
                text:text,
                buttons:[
                    {title:'关闭'}
                ]
            });
        },
        //confirm
        confirm:function(text,call){
            var self = this;
            this.$modal.show('dialog',{
                text:text,
                buttons:[
                    {
                        title: '取消',
                        handler:function(){
                            self.$modal.hide('dialog');
                            call(false);
                        }
                    },
                    {
                        title: '确认',
                        default: true,
                        handler:function(){
                            self.$modal.hide('dialog');
                            call(true);
                        }
                    }
                ]
            });
        }
    }
});